<div class="box">
		<div class="catname">
			<h2 class="jiaose-creat-ico">角色修改</h2>
		</div>
		<div class="jiaose-from backfff">
			<table width="90%" border="0" cellspacing="0" cellpadding="0" class="margin_ten">
				<tr>
					<td width="150"><i>*</i>角色名称</td>
					<td>{{.Role.Name}}</td><sapn style="color:red">{{.flash.error}}</span>
				</tr>
				<tr>
					<td valign="top">角色权限</td>
					<td>
						<ul>
							<li id="all-select"><label data-parent="all">全选/不全选</label><br class="clr" /></li>
							 {{range .Permissions}}
							<li id="all-pinglun"><label data-parent="y" id="per_{{.Id}}" checkId="{{.Id}}">{{.Description}}</label><br class="clr" />
								<ul>
									<li>
					                {{range .ChildPermissions}}								
											<input type="checkbox" id="permission_{{.Id}}"  class="hid" /><label id="per_{{.Id}}" checkId="{{.Id}}" level="level2" pid="{{.Pid}}">{{.Description}}</label>							
			               			 {{end}}
									</li>
								</ul>
							</li>		
		              	{{end}}
							</ul>
					</td>
				</tr>
				<tr>
					<td colspan="2" class="btn">
						<form action="/role/perm/{{.Role.Id}}" method="post" id="qxform">
							<input type="hidden" name="permissionIds" />
						</form>
						<input type="button" value="确定" />
						<button class="cancel" id="cancel">取消</button>
					</td>
				</tr>
			
			</table>
		</div>
	</div>
<script type="text/javascript">
  $(function () {
	{{range .RolePermissions}}
     	 $("#per_{{.permission_id}}").addClass("selected");
		 $("#permission_{{.permission_id}}").attr("value",1);
    {{end}}
	$(".jiaose-from label").click(function(){
		if (!$(this).data("parent")) {
			if ($(this).attr("class")) {
				$(this).removeClass("selected");
				$(this).parent().children("input[name='select']").attr("value",0);
			} else {
				$(this).addClass("selected");
				$(this).parent().children("input[name='select']").attr("value",1);
			}
			check_parent($(this).attr("for").split("-")[0]);
			check_all();
		} else {
			if ($(this).data("parent") == "y") {
				if ($(this).attr("class")) {
					$(this).removeClass("selected");
					$(this).parent().children("ul").children("li").children("label").removeClass("selected");
					$(this).parent().children("ul").children("li").children("input[name='select']").attr("value",0);
				} else {
					$(this).addClass("selected");
					$(this).parent().children("ul").children("li").children("label").addClass("selected");
					$(this).parent().children("ul").children("li").children("input[name='select']").attr("value",1);
				}
				check_all();
			}
			if ($(this).data("parent") == "all") {
				if ($(this).attr("class")) {
					$(".jiaose-from label").removeClass("selected");
					$(".jiaose-from input[name='select']").attr("value",0);
				} else {
					$(".jiaose-from label").addClass("selected");
					$(".jiaose-from input[name='select']").attr("value",1);
				}
			}
		}
	});
});

function check_parent(name){
	var parent_all = true;
	$("#all-" + name + " ul li label").each(function(){
		if (!$(this).attr("class")) {
			parent_all = false;
		}
	});

	if (parent_all) {
		$("#all-" + name).children("label").addClass("selected");
	} else {
		$("#all-" + name).children("label").removeClass("selected");
	}
}
function check_all(){
	var select_all = true;
	$(".jiaose-from label").each(function(){
		if (!$(this).attr("class") && $(this).data("parent") != "all") {
			select_all = false;
		}
	});

	if (select_all) {
		$("#all-select").children("label").addClass("selected");
	} else {
		$("#all-select").children("label").removeClass("selected");
	}
}
$("[type=button]").on("click",function(){
	$(".jiaose-from").find("label").each(function(){
		if($(this).attr("data-parent")!="all"){
				if($(this).attr("class")){
				var per=$("[name=permissionIds]").val()
				if(per==""||per==null){
					$("[name=permissionIds]").val($(this).attr("checkId"))
				}else{
					$("[name=permissionIds]").val(per+","+$(this).attr("checkId"))
				}
				
			}
		}
		
	});
	$("#qxform").submit()
});

	//子级选父级
	$("[level=level2]").on("click",function(){
		var e1=$(this).attr("pid")
		//$("[pid="+e1+"]").length
		//var flag=this.checked
		var i=0
		$("[pid="+e1+"]").each(function(){
			if(e1==$("[checkId="+e1+"]").attr("checkId")){
				$("[checkId="+e1+"]").addClass("selected");;
			}
			if(!this.checked){
				i++
			}
		});
		if($("[pid="+e1+"]").length==i){
			//勾选子元素最后一个 同时除去父级元素的勾选
			//$("[checkId="+e1+"]").attr("checked", false);
		}
		
	});
	$("#cancel").on("click",function(){
		window.history.back(-1);
	})
</script>